<cfcomponent>

<cffunction name="throwerror">
  <cfargument name="type" required="true" />
  <cfargument name="message" default="" />
	<cfthrow type="#arguments.type#" message="#arguments.message#" />
</cffunction>

<cffunction name="dumpvar">
  <cfargument name="var">
  <cfargument name="abort" default="true">
  <cftry>
<!---
 		<cfset class = createObject("java","java.lang.Class") />
		<cfset meths = var.getDeclaredMethods() />
		<cfset ms = structNew() />
		<cfloop from="1" to="#arrayLen(meths)#" index="m">
		  <cfset ms[meths[m].toString()] = structNew() />
		  <cfset ms[meths[m].toString()]["public"] = meths[m].public>
		  <cfset mps = meths[m].getParameterTypes()>
			<cfloop from="1" to="#arrayLen(mps)#" index="p">
			  <cfset ms[meths[m].toString()][mps[p].getName()] = mps[p].getName() />
			</cfloop>
		</cfloop>
	  <cfdump var="#ms#" />
--->
	  <cfdump var="#var#">
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>

	<cffunction name="newFile" hint="returns hibernate config object" access="public" returntype="any" output="false">
		<cfargument name="filepath" required="true" />
		<cfscript>
			var file = arguments.filepath;
			try {
				if(NOT directoryExists(getDirectoryFromPath(file))) {
					file = expandpath(file);
				}	
				else if(NOT directoryExists(getDirectoryFromPath(file))) {
					throwerror("urljavaloader.newfile.error","directory does not exist!");
				}
				return createObject("java", "java.io.File").init(file);
			}
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  dumpvar(excpt.message & " " & excpt.stacktrace);
			}
	
		</cfscript>
	</cffunction>


	<cffunction name="init" access="public" returntype="any" output="false" hint="hssql Constructor.">
		<cfargument name="libdirs" required="true" />
		<cfscript>
			var root = GetDirectoryFromPath(getMetaData(this).path);
			var library = ArrayNew(1);
			if(isArray(arguments.libdirs)) {
				library = arguments.libdirs;
			}
			else {
				for(x = 1; x lte listLen(arguments.libdirs);x = x+1) {
					libdir = listGetAt(arguments.libdirs,x);
					ArrayAppend(library, libdir);
					library = loadDirectory(libdir, library);
				}
			}
			//load the jars, create the creator
			variables._javaloader = "";
			makeLoader(library);
			return this;
		</cfscript>

	</cffunction>

	<cffunction name="dumpPackages" hint="Inititalises all the java libs to be loaded in" access="public" returntype="void" output="false">
		<cfscript>
			var package = createObject("java","java.lang.Package");
			var packages = package.getPackages();
			var allpacks = arrayNew(1);
			var pack = "";
			var packinfo = "";
			for (x = 1; x lte arrayLen(packages); x = x+1) {
				pack = packages[x];
				packinfo = structNew();
				packinfo["name"] = pack.getName();
				if(len(pack.getImplementationVersion())) {
					packinfo["version"] = pack.getImplementationVersion();
				}
				if(len(pack.getSpecificationVersion())) {
					packinfo["specver"] = pack.getSpecificationVersion();
				}
				if(len(pack.getSpecificationTitle())) {
					packinfo["title"] = pack.getSpecificationTitle();
				}
					
				allpacks[x] = packinfo;
			}
			dumpvar(allpacks);
		</cfscript>
	</cffunction>
	
	<cffunction name="loadDirectory" hint="Loads the JAR file paths into the array" access="public" returntype="array" output="false">
		<cfargument name="path" hint="The path to oad" type="string" required="Yes">
		<cfargument name="library" hint="The array of classpaths" type="array" required="Yes">
		<cfscript>
			var qJars = 0;
		</cfscript>
		<cfdirectory action="list" directory="#arguments.path#" filter="*.jar" name="qJars">	
		<cfloop query="qJars">
			<cfscript>
				ArrayAppend(arguments.library, arguments.path & name);
			</cfscript>
		</cfloop>	
		<cfscript>
			return arguments.library;
		</cfscript>
	</cffunction>
	
	<cffunction name="create" hint="Creates a hibernate object" access="public" returntype="any" output="false">
		<cfargument name="className" hint="The class name of the hiberanate class to load" type="string" required="Yes">
		<cfscript>
			var jloader = variables._javaloader;
			var clazz = "";
			try {
				clazz = jloader.loadClass(arguments.className);
				return clazz.newInstance();
			}
			catch(Any excpt) {
				// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
			  writeOutput("Create Error:<strong>" & arguments.className & "</strong>:" & excpt.message & " " & excpt.stacktrace);
			  loadedjars = jloader.getURLs();
			  for(x = 1; x lte arrayLen(loadedjars); x = x+1) {
			  	writeOutput(loadedjars[x].getFile() & "<br />");
			  }
			  dumpvar("borted");
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="fakeinit" access="public" returntype="any" output="false">
		<cfreturn this />
	</cffunction>
	

	<cffunction name="makeLoader" access="private" returntype="void" output="false">
		<cfargument name="jarsarray" required="true" type="array" />
		<cfscript>
			var Class = createObject("java", "java.lang.Class");
			var Array = createObject("java", "java.lang.reflect.Array");
			var iterator = arguments.jarsarray.iterator();
			var file = 0;
			var urls = Array.newInstance(Class.forName("java.net.URL"), ArrayLen(jarsarray));
			var counter = 0;
	//		dumpvar(jarsarray);
			try {
				while(iterator.hasNext())
				{
					Array.set(urls, javacast("int",counter), newFile(iterator.next()).toURL());
					counter = counter + 1;
				}
				classLoader = createObject("java", "java.net.URLClassLoader").init(urls);
			}
			catch(Any excpt) {
				throwerror("urljavaloader.makeloader.error","Error:" & excpt.message & " " & excpt.stacktrace);
			}	
	//		classLoader = createObject("java", "ChildFirstClassLoader").init(urls);
			variables._javaloader = classLoader;
		</cfscript>
	</cffunction>


</cfcomponent>